package com.twotiger.and.activity.current;

import java.util.HashMap;

import android.content.Intent;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import app.twotiger.p2p.R;

import com.alibaba.fastjson.JSONObject;
import com.twotiger.and.Constants;
import com.twotiger.and.activity.HomeActivity;
import com.twotiger.and.activity.base.BaseActivity;
import com.twotiger.and.activity.base.BaseHandler;
import com.twotiger.and.activity.base.SubTitle;
import com.twotiger.and.bean.Basebean;
import com.twotiger.and.bean.CurrentEnter;
import com.twotiger.and.bean.Share;
import com.twotiger.and.util.IntentUtil;
import com.twotiger.and.util.UmShearUtils;
import com.twotiger.and.util.ViewUtils;

/**
 * @Author gao
 * @Date 2015年8月12日 下午2:50:58
 */
public class CurrentEnterSuccessPage extends BaseActivity implements OnClickListener {

    private static final int GETSHARESUCCESS = 0;
    private static final int GETSHAREFAILURE = 1;
    
    private Button btn_share;
    private HashMap<String, String> parm;
    private SubTitle subTitle;
    private CurrentEnter mCurrentEnterSuc;
    private ImageView iv_icon;
    private TextView tv_msg, tv_reason, tv_tips_1;
    private LinearLayout ll_isshow;
    private TextView tv_current_assert;
    private Basebean mCurrentEnterFail;

    @Override
    protected View loadViewLayout(LayoutInflater inflater) {
        View view = inflater.inflate(R.layout.current_enter_success_layout, null);
        return view;
    }

    @Override
    protected void findViewById(View view) {
        subTitle = new SubTitle(view) {
            @Override
            protected void clickTitle(View v) {
            }

            @Override
            protected void clickRightBtn(View v) {
            }

            @Override
            protected void cickLeftBtn(View v) {
                onBackPress();
            }
        };
        subTitle.setTitle("转入成功");
        subTitle.hideBtns();
        subTitle.title_left_bt.setVisibility(View.VISIBLE);

        btn_share = (Button) view.findViewById(R.id.btn_share);
        iv_icon = (ImageView) view.findViewById(R.id.iv_icon);
        tv_msg = (TextView) view.findViewById(R.id.tv_msg);
        tv_reason = (TextView) view.findViewById(R.id.tv_reason);
        tv_tips_1 = (TextView) view.findViewById(R.id.tv_tips_1);
        ll_isshow = (LinearLayout) view.findViewById(R.id.ll_isshow);
        tv_current_assert = (TextView) view.findViewById(R.id.tv_current_assert);

    }

    @Override
    public void onClick(View view) {

        if (view == null) {
            return;
        }

        switch (view.getId()) {
        case R.id.tv_current_assert:
            setResult(CurrentHost.CURRENTRESULTCLOSE_SUC);
            finish();
            overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
            break;
        case R.id.btn_share:
            if(mCurrentEnterSuc != null){
                gotoShare();
            }else{
                startNewActivity(new Intent(CurrentEnterSuccessPage.this, HomeActivity.class).putExtra(
                    "BUTTON", R.id.radio_btn2), R.anim.push_left_in, R.anim.push_left_out, true);
            }
            break;
        }
    }
    /**
     * 分享
     */
    protected void gotoShare() {
        parm = getParm();
        parm.clear();
        parm.put("type", "inProjectX");
        parm.put("token", getToken());
        getDate(parm, Constants.SHARE, handler, GETSHARESUCCESS, GETSHAREFAILURE, true, false, false);
    }
    @Override
    protected void setListener() {
        btn_share.setOnClickListener(this);
        tv_current_assert.setOnClickListener(this);

        handler = new BaseHandler(this) {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                case GETSHARESUCCESS:
                    Basebean result_s = JSONObject.parseObject((String) msg.obj, Basebean.class);
                    if(result_s.isOk()){
                        Share shareInfo = JSONObject.parseObject(result_s.data, Share.class);
                        String title = shareInfo.getTitle();
                        String content = shareInfo.getContent();
                        String url = shareInfo.getUrl();
                        String pic = shareInfo.getPic();
                        UmShearUtils.shear(CurrentEnterSuccessPage.this, url, true, title, content,pic);
                    }else{
                        showToast(result_s.codeDesc);
                    }
                    break;
                case GETSHAREFAILURE:
                    showToast("网络异常");
                    break;
                default:
                    break;
                }
                super.handleMessage(msg);
            }
        };
    }

    @Override
    protected void processLogic() {
        parm = getParm();
        mCurrentEnterSuc = (CurrentEnter) getIntent().getSerializableExtra("CURRENTENTERSUC");
        mCurrentEnterFail = (Basebean) getIntent().getSerializableExtra("CURRENTENTERFAIL");

        if (mCurrentEnterSuc != null) {
            showSuccessPage();
        } 
        if(mCurrentEnterFail != null) {
            showFailPage();
        }
    }

    private void showFailPage() {
        subTitle.setTitle("转入失败");
        iv_icon.setImageResource(R.drawable.invest_fail);
        tv_msg.setText("转入失败！");
//        tv_reason.setText("由于您操作过于频繁，出于安全考虑，\n您今天暂时不可转入啦，明天再试吧。");
        if(mCurrentEnterFail != null){
            tv_reason.setText(mCurrentEnterFail.codeDesc);
        }else{
            tv_reason.setText("失败了！");
        }
        btn_share.setText("看看其他项目");
        tv_tips_1.setText("如有疑问，欢迎拨打客服电话："+Constants.PHONE_NUM_FORMAT);
        tv_tips_1.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                if (ViewUtils.isFastDoubleClick(v)) {
                    return;
                }
                IntentUtil.CallPhone(CurrentEnterSuccessPage.this, Constants.PHONE_NUM);
            }
        });
        ll_isshow.setVisibility(View.VISIBLE);
    }

    private void showSuccessPage() {
        iv_icon.setImageResource(R.drawable.invest_suc);
        tv_msg.setText("转入成功！");
        tv_reason.setText("明日即可查看今日收益");
        btn_share.setText("炫耀一下");
        if(mCurrentEnterSuc.getCount() >= mCurrentEnterSuc.getMaxCount() - 2){
            tv_tips_1.setText("由于您操作过于频繁，处于安全考虑，您今日可转入次数还剩  "+(mCurrentEnterSuc.getMaxCount()-mCurrentEnterSuc.getCount())+" 次");
            ll_isshow.setVisibility(View.VISIBLE);
        }
    }

    @Override
    protected void onBackPress() {
        if(mCurrentEnterSuc != null){
            setResult(CurrentHost.CURRENTRESULTCLOSE_SUC);
        }
        if(mCurrentEnterFail != null){
            setResult(CurrentHost.CURRENTRESULTCLOSE_FAI);
        }
        finish();
        overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
    }

}
